<?php

declare (strict_types=1);
namespace App\Model;

use Hyperf\DbConnection\Model\Model;
/**
 * @property int $id 
 * @property int $user_id 
 * @property int $dy_id
 * @property int $channel_id 
 * @property \Carbon\Carbon $created_at 
 * @property \Carbon\Carbon $updated_at 
 */
class OcpcDyUser extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'ocpc_dy_user';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'integer', 'user_id' => 'integer', 'dy_id' => 'integer', 'channel_id' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];

    /** 查询关联用户ID
     * @param int $userId
     * @param $channel
     * @return array
     */
    public static function getUrlByUserId(int $userId, $channel):array
    {
        $res = self::where('user_id', $userId)->where('channel_id', $channel)->first();

        if (!empty($res)) {
            return $res->toArray();
        }

        return [];
    }

    /**
     * 添加关联用户
     * @param $data
     * @return int
     */
    public static function addRelation($data)
    {
        $data['created_at'] = $data['updated_at'] = date('Y-m-d H:i:s');

        return self::insertGetId($data);
    }
}